/**
 * 表单验证
 * 
 * @param {Object} rules 验证字段的规则
 * @param {Object} messages 验证字段的提示信息
 * 
 */

import basic from './basic.js';
var actmodel = new basic();
actmodel.do="integralpro_contents"
actmodel.testlinke=function(){
  var app = getApp();
  var data = { "op": "testlinke" ,plugin:'integral'}
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/'+actmodel.do,
      'data': data,
      'method': "get",
      showLoading: false,
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });
},
actmodel.getmodel=function(id,activityid){
  var app = getApp();
  var data = { "op": "getproinfo", id,activityid,plugin:'integral' }
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/'+actmodel.do,
      'data': data,
      'method': "get",
      showLoading: false,
      success: function (res) {
        if (res.data.errno == 0) {
          resolve(res.data.data);
        }
        reject(res.data.message);
      },
      fail: function (res) {
        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {
      }
    });
  });
}

actmodel.showattrchose=function(that,proid,selected,op,options){
  var app = getApp(); 
  
  if(!options)
  {
    options={}
  }
  options["proid"]=proid
  options['selected'] = selected;
  options['type'] = op;  
  app.xc_sys_config.authop.oppower("pro").then(function (res) {
    var proattrchoose = that.selectComponent("#proattrchoose");   
     
    proattrchoose.setpro(options);       
  })
}
actmodel.showproinfo = function (id, activityid, activitytype) {
  var app = getApp();
  var data = {
    "id": id,
    "op": "showproinfo",
    'activityid': activityid,
    'activitytype': activitytype,
    plugin:'integral' 
  }
  return new Promise((resolve, reject) => {
    app.util.request({
      'url': 'entry/wxapp/'+actmodel.do,
      'data': data,
      'method': "get",
      showLoading: false,
      success: function (res) {
        if (res.data.errno == 0) {
          // var imgslist = res.data.data["obj"]["imgslist"];

          if (res.data.data["obj"]["guigetype"] != "-1") {
            res.data.data["obj"]["skuht"] = sku(res.data.data["obj"]["skudata"]);
          }
       
          resolve(res.data.data);

        }
        reject(res.data.message);
      },
      fail: function (res) {

        reject(((res.data && res.data.message) ? res.data.message : res.errMsg));
      },
      complete: function () {}
    });
  });
}
actmodel.offset = function (that, reset, para, xcsorts) { 
  if(!xcsorts)
  {
    xcsorts=[["id", "desc"], ["createtime", "desc"]] ;
  }
 
  var acturlpara = {};   
  acturlpara["url"] = "entry/wxapp/"+actmodel.do
  if (para) {
    para["op"] = "offset"
    para["plugin"]='integral'
  }
  else {
    this.wherepara["op"] = "offset"
  }
  return actmodel.pagelist(acturlpara, that, reset, para, xcsorts);
}


function sku(sku) {
  var arrs = [];
  for (var i = 0; i < sku.grouplabel.length; i++) {
    var attr = [];
    for (var n = 0; n < sku.grouplabel[i]["attrs"].length; n++) {
      attr.push(n);
    }
    arrs.push(attr);
  }
  //array.fill(value, start, end)
  var cartesianproduct = Array.prototype.reduce.call(arrs, function (a, b) {
    var ret = [];
    a.forEach(function (a) {
      b.forEach(function (b) {
        ret.push(a.concat([b]));
      });
    });
    return ret;
  }, [
    []
  ]);
  var skuht = {};
  sku.stockdata.forEach(function (val, i) {
    skuht[cartesianproduct[i].join("_")] = {
      stock: parseInt(val["stock"]),
      isshow: val["isshow"],
      weight: val["weight"],
      isdefault: val["isdefault"],
      salesvolume: val["salesvolume"],
      simg: val["simg"],
      price: val["price"],
      realprice: val["realprice"],
      activityprice: val["activityprice"]
    }
  })
  return skuht;
}
module.exports = actmodel;